#!/usr/bin/env python

import component

from _group import Group
from autorm.fields import *

class Contact(component.Model):
    '''Contact class
    
    WARNING!
    This table using modified Autorm with collate.
    
    @author Julot <andy.julot@gmail.com>
    @since Thursday, February 23, 2012. 09:05 AM'''
    
    class Meta:
        table = 'contacts'
        pk = 'id'
        fields = [IdField('id'),
                  ISODateTimeField('insertTime'),
                  ISODateTimeField('updateTime'),
                  TextField('name', 255, index=True, collate='NOCASE'),
                  TextField('address', 255),
                  TextField('city', 255, index=True),
                  TextField('province', 255, index=True),
                  CsvField('phones'),
                  CsvField('groups')]
    
    _oldGroups = None
    
    def __setattr__(self, name, value):
        if name == 'groups':
            if self._oldGroups == None:
                try:
                    self._oldGroups = self.groups
                except: pass
        super(Contact, self).__setattr__(name, value)
        
    def save(self):
        r = super(Contact, self).save()
        Group.updateFrequency(self._oldGroups, self.groups)

    